<template>
  <div id="addPicturePage">
    <h2 style="margin-bottom: 16px">
      {{ route.query?.id ? '修改图片' : '创建图片' }}
    </h2>

    <a-typography-paragraph v-if="spaceId" type="secondary">
      保存至空间：<a :href="`/space/${spaceId}`" target="_blank">{{ spaceId }}</a>
    </a-typography-paragraph>

    <a-tabs v-model:activeKey="uploadType"
    >>
      <a-tab-pane key="file" tab="文件上传">
        <PictureUploadPage :picture="picture" :space-id="spaceId as any" :onSuccess="onSuccess" />
      </a-tab-pane>
      <a-tab-pane key="url" tab="URL 上传" force-render>
        <UrlPIctureUploadPage :picture="picture" :space-id="spaceId as any" :onSuccess="onSuccess" />
      </a-tab-pane>
    </a-tabs>

    <a-form v-if="picture!=null" class="form" layout="vertical" :model="pictureForm" @finish="handleSubmit">
      <a-form-item label="名称" name="name">
        <a-input v-model:value="pictureForm.name" placeholder="请输入名称" />
      </a-form-item>
      <a-form-item label="简介" name="introduction">
        <a-textarea
          v-model:value="pictureForm.introduction"
          placeholder="请输入简介"
          :rows="2"
          autoSize
          allowClear
        />
      </a-form-item>
      <a-form-item label="分类" name="category">
        <a-auto-complete
          v-model:value="pictureForm.category"
          placeholder="请输入分类"
          :options="categoryOptions"
          allowClear
        />
      </a-form-item>
      <a-form-item label="标签" name="tags">
        <a-select
          v-model:value="pictureForm.tags"
          mode="tags"
          placeholder="请输入标签"
          :options="tagOptions"
          allowClear
        />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" html-type="submit" style="width: 100%">提交</a-button>
      </a-form-item>
    </a-form>

  </div>

</template>

<script setup lang="ts">
import PictureUploadPage from '@/components/PictureUploadPage.vue'
import { computed, onMounted, reactive, ref } from 'vue'
import { editPictureUsingPost, getPictureVoByIdUsingGet, getTagAndCategoryUsingGet } from '@/api/pictureController.ts'
import { message } from 'ant-design-vue'
import { useRoute, useRouter } from 'vue-router'
import UrlPIctureUploadPage from '@/components/UrlPIctureUploadPage.vue'

const uploadType = ref<'file' | 'url'>('file')

const picture = ref<API.PictureVO>()

const onSuccess = (newPicture: API.PictureVO) => {
  picture.value = newPicture
  pictureForm.name = newPicture.name
  pictureForm.introduction = newPicture.introduction
  pictureForm.category = newPicture.category
  pictureForm.tags = newPicture.tags
}

const pictureForm = reactive<API.PictureEditRequest>({})

const router = useRouter()
// 空间 id
const spaceId = computed(() => {
  return route.query?.spaceId
})


const handleSubmit = async (values: any) => {

  const pictureId = picture.value.id
  // if (!pictureId) return

  const res = await editPictureUsingPost({
    id: pictureId,
    spaceId: spaceId.value,
    ...values
  })
  if (res.data.code === 0 && res.data.data) {
    message.success('创建成功！')
    await router.push({
      path: `/picture/${pictureId}`
    })
  } else {
    message.error('创建失败' + res.data.message)
  }
}

const categoryOptions = ref<string[]>([])
const tagOptions = ref<string[]>([])

// 获取标签和分类选项
const getTagCategoryOptions = async () => {
  const res = await getTagAndCategoryUsingGet()
  if (res.data.code === 0 && res.data.data) {
    // 转换成下拉选项组件接受的格式
    tagOptions.value = (res.data.data.tagList ?? []).map((data: string) => {
      return {
        value: data,
        label: data
      }
    })
    categoryOptions.value = (res.data.data.categoryList ?? []).map((data: string) => {
      return {
        value: data,
        label: data
      }
    })
  } else {
    message.error('加载选项失败，' + res.data.message)
  }
}

const route = useRoute()
const pictureDetail = async () => {
  const id = route.query?.id
  if (!id) return
  const res = await getPictureVoByIdUsingGet({ id })
  if (res.data.code === 0 && res.data.data) {
    picture.value = res.data.data
    pictureForm.name = res.data.data.name
    pictureForm.introduction = res.data.data.introduction
    pictureForm.category = res.data.data.category
    pictureForm.tags = res.data.data.tags

  }
}

onMounted(() => {
  pictureDetail()
  getTagCategoryOptions()
})

</script>

<style scoped>

#addPicturePage {
  margin-top: 20px;
}

#addPicturePage .form {

  max-width: 720px;
  margin: 0 auto;
}
</style>
